package com.gao.config;

import com.gao.shiro.jwt.filter.JwtFilter;
import com.gao.shiro.shiro.MyRealm;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author anshenghui
 * @Date 2020/1/15 15:59
 * @Version 1.0
 */
@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean getFactoryBean(DefaultSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //添加jwt拦截器
        Map<String, Filter> filterMap = new HashMap<>();
        filterMap.put("jwt", new JwtFilter());
        shiroFilterFactoryBean.setFilters(filterMap);

        //顺序执行  拦截接口
        Map<String, String> map = new LinkedHashMap<>();
        map.put("/doc.html", "anon");
        map.put("/**/*.js", "anon");
        map.put("/**/*.html", "anon");
        map.put("/**/*.css", "anon");
        map.put("/**/*.ico", "anon");
        map.put("/swagger**/**", "anon");
        map.put("/v2/**", "anon");
        map.put("/login/getCode", "anon");
        map.put("/login/doLogin", "anon");
        map.put("/**", "jwt");
//        shiroFilterFactoryBean.setUnauthorizedUrl("/SysUser/list");
//        shiroFilterFactoryBean.setLoginUrl("/SysUser/list");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    @Bean(name = "securityManager")
    public DefaultSecurityManager getManager(MyRealm myRealm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(myRealm);
        return defaultWebSecurityManager;
    }

    @Bean(name = "myRealm")
    public MyRealm getRealm() {
        return new MyRealm();
    }

}
